﻿namespace HRM.CORE.REPORT
{
    using DevExpress.Utils;
    using DevExpress.XtraPrinting;
    using DevExpress.XtraReports.UI;
    using Dictionary.ERP;
    using HRM.CORE;
    using HRM.CORE.CLASS;
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlTypes;
    using System.Drawing;
    using System.Drawing.Printing;

    public class rptAutoListEmployee : XtraReport
    {
        private BottomMarginBand BottomMargin;
        private IContainer components;
        private DetailBand Detail;
        private XRLabel dtDate;
        private GroupHeaderBand GroupHeader1;
        private HRM_EMPLOYEE l_Employee;
        private string m_Code;
        private DataTable m_DataTable;
        private int m_Level;
        private int m_Order;
        private string m_Title;
        private ReportFooterBand ReportFooter;
        private ReportHeaderBand ReportHeader;
        private TopMarginBand TopMargin;
        private XRLabel txtTitle;
        private XRLabel txtUnit;

        public rptAutoListEmployee()
        {
            this.l_Employee = new HRM_EMPLOYEE();
            this.m_Level = 0;
            this.m_Code = "";
            this.m_Title = "";
            this.m_Order = 0;
            this.components = null;
            this.InitializeComponent();
        }

        public rptAutoListEmployee(int Level, string Code, DataTable DataTable, string Title)
        {
            this.l_Employee = new HRM_EMPLOYEE();
            this.m_Level = 0;
            this.m_Code = "";
            this.m_Title = "";
            this.m_Order = 0;
            this.components = null;
            this.InitializeComponent();
            this.m_Level = Level;
            this.m_Code = Code;
            this.m_Title = Title;
            this.m_DataTable = DataTable;
        }

        private void chSex_BeforePrint(object sender, PrintEventArgs e)
        {
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void dtBirthday_BeforePrint(object sender, PrintEventArgs e)
        {
        }

        private void InitializeComponent()
        {
            this.Detail = new DetailBand();
            this.TopMargin = new TopMarginBand();
            this.dtDate = new XRLabel();
            this.BottomMargin = new BottomMarginBand();
            this.ReportHeader = new ReportHeaderBand();
            this.txtTitle = new XRLabel();
            this.txtUnit = new XRLabel();
            this.GroupHeader1 = new GroupHeaderBand();
            this.ReportFooter = new ReportFooterBand();
            this.BeginInit();
            this.Detail.HeightF = 29.16667f;
            this.Detail.Name = "Detail";
            this.Detail.Padding = new PaddingInfo(0, 0, 0, 0, 100f);
            this.Detail.TextAlignment = TextAlignment.TopLeft;
            this.TopMargin.Controls.AddRange(new XRControl[] { this.dtDate });
            this.TopMargin.HeightF = 31f;
            this.TopMargin.Name = "TopMargin";
            this.TopMargin.Padding = new PaddingInfo(0, 0, 0, 0, 100f);
            this.TopMargin.TextAlignment = TextAlignment.TopLeft;
            this.dtDate.LocationFloat = new PointFloat(851.2709f, 10.00002f);
            this.dtDate.Name = "dtDate";
            this.dtDate.Padding = new PaddingInfo(2, 2, 0, 0, 100f);
            this.dtDate.SizeF = new SizeF(187.7291f, 17.99998f);
            this.dtDate.StylePriority.UseTextAlignment = false;
            this.dtDate.Text = "dtDate";
            this.dtDate.TextAlignment = TextAlignment.MiddleRight;
            this.BottomMargin.HeightF = 56f;
            this.BottomMargin.Name = "BottomMargin";
            this.BottomMargin.Padding = new PaddingInfo(0, 0, 0, 0, 100f);
            this.BottomMargin.TextAlignment = TextAlignment.TopLeft;
            this.ReportHeader.Controls.AddRange(new XRControl[] { this.txtTitle, this.txtUnit });
            this.ReportHeader.HeightF = 55f;
            this.ReportHeader.Name = "ReportHeader";
            this.txtTitle.Font = new Font("Times New Roman", 16f, FontStyle.Bold);
            this.txtTitle.LocationFloat = new PointFloat(0f, 0f);
            this.txtTitle.Name = "txtTitle";
            this.txtTitle.Padding = new PaddingInfo(2, 2, 0, 0, 100f);
            this.txtTitle.SizeF = new SizeF(1039f, 30.25001f);
            this.txtTitle.StylePriority.UseFont = false;
            this.txtTitle.StylePriority.UseTextAlignment = false;
            this.txtTitle.Text = "DANH S\x00c1CH NH\x00c2N VI\x00caN";
            this.txtTitle.TextAlignment = TextAlignment.MiddleCenter;
            this.txtUnit.LocationFloat = new PointFloat(0f, 30.25001f);
            this.txtUnit.Name = "txtUnit";
            this.txtUnit.Padding = new PaddingInfo(2, 2, 0, 0, 100f);
            this.txtUnit.SizeF = new SizeF(1039f, 17.70834f);
            this.txtUnit.StylePriority.UseTextAlignment = false;
            this.txtUnit.TextAlignment = TextAlignment.MiddleCenter;
            this.GroupHeader1.HeightF = 28.125f;
            this.GroupHeader1.Name = "GroupHeader1";
            this.GroupHeader1.RepeatEveryPage = true;
            this.ReportFooter.HeightF = 10f;
            this.ReportFooter.Name = "ReportFooter";
            base.Bands.AddRange(new Band[] { this.Detail, this.TopMargin, this.BottomMargin, this.ReportHeader, this.GroupHeader1, this.ReportFooter });
            base.Font = new Font("Times New Roman", 9.75f);
            base.Landscape = true;
            base.Margins = new Margins(0x47, 0x3b, 0x1f, 0x38);
            base.PageHeight = 0x33b;
            base.PageWidth = 0x491;
            base.PaperKind = PaperKind.A4;
            base.Version = "9.3";
            this.BeforePrint += new PrintEventHandler(this.rptAutoListEmployee_BeforePrint);
            this.EndInit();
        }

        private void LoadLevel()
        {
            if (this.m_Level == 1)
            {
                HRM_BRANCH hrm_branch = new HRM_BRANCH();
                hrm_branch.Get(this.m_Code);
                this.txtUnit.Text = "Đơn vị: " + hrm_branch.BranchName;
            }
            else if (this.m_Level == 2)
            {
                HRM_DEPARTMENT hrm_department = new HRM_DEPARTMENT();
                hrm_department.Get(this.m_Code);
                this.txtUnit.Text = "Đơn vị: " + hrm_department.DepartmentName;
            }
            else if (this.m_Level == 3)
            {
                HRM_GROUP hrm_group = new HRM_GROUP();
                hrm_group.Get(this.m_Code);
                this.txtUnit.Text = "Đơn vị: " + hrm_group.GroupName;
            }
        }

        private void LoadListEmployee()
        {
            this.txtTitle.Text = this.m_Title;
            XRTable child = new XRTable();
            XRTable table2 = new XRTable();
            XRTableRow row = new XRTableRow();
            XRTableRow row2 = new XRTableRow();
            foreach (DataRow row3 in this.m_DataTable.Rows)
            {
                XRTableCell cell = new XRTableCell {
                    Text = row3["FieldName"].ToString(),
                    WidthF = float.Parse(row3["Width"].ToString()),
                    TextAlignment = TextAlignment.MiddleCenter,
                    Font = new Font("Times New Roman", 12f, FontStyle.Bold)
                };
                row.Cells.Add(cell);
                XRTableCell DetailTableCell = new XRTableCell {
                    Text = "[" + row3["FieldCode"].ToString() + "]",
                    WidthF = float.Parse(row3["Width"].ToString())
                };
                if (int.Parse(row3["Position"].ToString()) == 0)
                {
                    DetailTableCell.TextAlignment = TextAlignment.MiddleLeft;
                    DetailTableCell.Padding = new PaddingInfo(4, 0, 0, 0, 100f);
                }
                else if (int.Parse(row3["Position"].ToString()) == 1)
                {
                    DetailTableCell.TextAlignment = TextAlignment.MiddleCenter;
                }
                else
                {
                    DetailTableCell.TextAlignment = TextAlignment.MiddleRight;
                    DetailTableCell.Padding = new PaddingInfo(0, 4, 0, 0, 100f);
                }
                if (row3["FieldCode"].ToString() == "Birthday")
                {
                    DetailTableCell.Text = "[BirthdayDay]/[BirthdayMonth]/[BirthdayYear]";
                }
                if (row3["FieldCode"].ToString() == "Sex")
                {
                    DetailTableCell.BeforePrint += delegate (object s, PrintEventArgs e) {
                        if (!bool.Parse(DetailTableCell.Text))
                        {
                            DetailTableCell.Text = "Nữ";
                        }
                        else
                        {
                            DetailTableCell.Text = "Nam";
                        }
                    };
                }
                else
                {
                    DetailTableCell.BeforePrint += delegate (object s, PrintEventArgs e) {
                        DateTime now = DateTime.Now;
                        bool result = false;
                        if (DateTime.TryParse(DetailTableCell.Text, out now))
                        {
                            if (Convert.ToDateTime(DetailTableCell.Text) == Convert.ToDateTime(SqlDateTime.MaxValue.ToString()))
                            {
                                DetailTableCell.Text = "";
                            }
                            else
                            {
                                DetailTableCell.Text = Convert.ToDateTime(DetailTableCell.Text).ToShortDateString();
                            }
                        }
                        if (bool.TryParse(DetailTableCell.Text, out result))
                        {
                            if (!bool.Parse(DetailTableCell.Text))
                            {
                                DetailTableCell.Text = "";
                            }
                            else
                            {
                                DetailTableCell.Text = "X";
                            }
                        }
                    };
                }
                row2.Cells.Add(DetailTableCell);
            }
            child.Rows.Add(row);
            child.WidthF = (base.PageWidth - base.Margins.Left) - base.Margins.Right;
            child.Borders = BorderSide.All;
            this.GroupHeader1.HeightF = child.HeightF;
            this.GroupHeader1.Controls.Add(child);
            table2.Rows.Add(row2);
            table2.WidthF = (base.PageWidth - base.Margins.Left) - base.Margins.Right;
            table2.Borders = BorderSide.Bottom | BorderSide.Right | BorderSide.Left;
            this.Detail.HeightF = table2.HeightF;
            this.Detail.Controls.Add(table2);
            DataSet set = new DataSet();
            DataTable table = new DataTable();
            table = this.l_Employee.GetListCurrentNow(this.m_Level, this.m_Code, 1);
            set.Tables.Add(table);
            base.DataSource = set;
            base.DataMember = table.TableName;
            base.DataAdapter = table;
        }

        private void rptAutoListEmployee_BeforePrint(object sender, PrintEventArgs e)
        {
            this.LoadLevel();
            this.LoadListEmployee();
            this.dtDate.Text = DateTime.Now.ToShortDateString();
            CreateHeaderReport.CreateHeader(this.ReportHeader);
            CreateFooterReport.CreateFooter("IsOther", this.ReportFooter, this);
        }

        private void txtOrder_BeforePrint(object sender, PrintEventArgs e)
        {
            this.m_Order++;
        }
    }
}

